package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService{

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;


    /**
     * @param setmealDTO:
     * @return void
     * @description 新增套餐和对应的口味
     */
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //向套餐表插入数据（保存套餐信息）
        setmealMapper.insert(setmeal);

        //获取insert语句生成的主键值
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes!= null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            //向套餐菜品表插入n条数据
            setmealDishMapper.insertBatchDishes(setmealDishes);
        }

    }

    /**
     * @param setmealPageQueryDTO:
     * @return PageResult
     * @description 套餐分页查询
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * @param ids:
     * @return Result
     * @description 菜品批量删除，同时删除套餐和菜品的关联数据
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //1.先判断套餐是不是可以删除-----起售状态不可以删除
        //select count(*) from setmeal where id in(1,2,3) and status=1
        ids.forEach(id->{
            Setmeal setmeal=setmealMapper.getById(id);
            if(setmeal.getStatus()==StatusConstant.ENABLE){
                //起售状态不可以删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        //2.来到这，说明可以删除
        ids.forEach(id->{
            //2.1先删除套餐表的数据
            //delete from setmeal where setmeal_id in(1,2,3)
            setmealMapper.deleteById(id);
            //2.2再删除套餐菜品关系表中的数据
            setmealDishMapper.deleteBySetmealId(id);

        });



    }

    /**
     * @param id:
     * @return SetmealVO
     * @description 根据id查询套餐和对应的菜品
     */
    public SetmealVO getByIdWithDish(Long id) {
        //根据id查询套餐数据
        Setmeal setmeal=setmealMapper.getById(id);

        //根据套餐id查找菜品数据
        List<SetmealDish> setmealDishes=setmealDishMapper.getDishBySetmealId(id);

        //将查询到的数据封装成VO
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * @param setmealDTO:
     * @return void
     * @description 修改套餐和对应菜品数据
     */
    public void updateWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //修改套餐表数据
        setmealMapper.update(setmeal);

        //删除原有的菜品数据
        setmealDishMapper.deleteBySetmealId(setmeal.getId());

        //重新插入菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });

        //重新插入套餐和菜品的关联关系
        setmealDishMapper.insertBatchDishes(setmealDishes);

    }

    /**
     * @param status:
     * @param id:
     * @return void
     * @description 起售停售套餐
     */
    public void startOrStop(Integer status, Long id) {
        //1.起售套餐时，先判断套餐内是否有停售菜品
        if(status==StatusConstant.ENABLE){
            //1.1根据套餐id查询套餐中包含哪些菜品
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            //1.2判断菜品是不是停售状态
            if(dishList!=null&&dishList.size()>0){
                dishList.forEach(dish -> {
                    if(dish.getStatus()==StatusConstant.DISABLE){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal=Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        setmealMapper.update(setmeal);

    }

    /**
     * @param setmeal:
     * @return List<Setmeal>
     * @description 条件查询
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

   /**
    * @param id:套餐id
    * @return List<DishItemVO>
    * @description 根据id查询菜品选项
    */
    public List<DishItemVO> getDishItemById(Long id) {

        return setmealMapper.getDishItemBySetmealId(id);
    }
}